package com.song.ejf.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SRole;
import com.song.ejf.service.ResourceGroupService;
import com.song.ejf.service.RoleService;
import com.song.ejf.util.DateUtil;
import com.song.ejf.util.JSONUtil;
import com.song.ejf.util.UUIDUtil;

public class RoleAction extends ActionSupport {

	private static final long serialVersionUID = -6609393044471160489L;
	Log log = LogFactory.getLog(RoleAction.class);
	public SRole role;
	public String checkItems;
	public Page page;
	public List<Map<String, String>> allResGroup;
	public List<Map<String, String>> noGroupedRes;
	public String allRolesRes;
	public String allGroupedRes;
	@Autowired()
	public RoleService roleService;
	@Autowired()
	public ResourceGroupService resourceGroupService;
	
	/**
	 * 准备配置角色功能
	 * @return
	 */
	public String pConfigRoleRes(){
		try {
			role = roleService.findRoleById(role.getRId());
			allResGroup = resourceGroupService.loadAllResGroup();
			allGroupedRes = JSONUtil.convertObj2Json(resourceGroupService.loadAllGroupedRes());
			noGroupedRes = resourceGroupService.loadAllNoGroupRes();
			allRolesRes = JSONUtil.convertObj2Json(roleService.loadResByRoleId(role.getRId()));
		} catch (Exception e) {
			log.error("跳转到配置角色资源页面异常.",e);
		}
		return "pConfigRoleRes";
	}
	/**
	 * 配置角色功能
	 * @return
	 */
	public String configRoleRes(){
		try {
			if(checkItems == null){
				addActionMessage("配置角色资源失败.选择资源出错.");
				return "configRoleRes";
			}
			if(roleService.addResRole(checkItems.split(","), role.getRId())){
				addActionMessage("配置角色资源成功.");
			}else{
				addActionMessage("配置角色资源失败.");
			}
		} catch (Exception e) {
			log.error("配置角色资源异常.",e);
		}
		return "configRoleRes";
	}
	/**
	 * 准备添加角色
	 * @return
	 */
	public String pAddRole(){
		return "pAddRole";
	}
	/**
	 * 添加角色.
	 * @return
	 */
	public String addRole(){
		try {
			if(role != null){
				role.setLastModifyTime(DateUtil.formatDate());
				role.setRId(UUIDUtil.makeUUID());
				roleService.saveOrUpdateRole(role);
				addActionMessage("添加角色成功!");
			}else{
				addActionMessage("添加角色失败!");
			}
		} catch (Exception e) {
			log.error("添加角色异常.",e);
		}
		return "addRole";
	}
	/**
	 * 列出所有角色
	 * @return
	 */
	public String listRole(){
		try {
			if(page == null){
				page = new Page();
			}
			if(role == null){
				role = new SRole();
			}
			roleService.loadRolePage(page, role.getRoleName());
		} catch (Exception e) {
			log.error("资源列表异常.",e);
		}
		return "listRole";
	}
	/**
	 * 删除角色
	 * @return
	 */
	public String delRole(){
		try {
			if(StringUtils.isBlank(checkItems)){
				addActionMessage("删除角色失败!");
				return "delRole";
			}
			boolean flag = roleService.delRole(checkItems.split(","));
			if(flag){
				addActionMessage("删除角色成功!");
			}else{
				addActionMessage("删除角色失败!");
			}
		} catch (Exception e) {
			log.error("删除角色异常.",e);
		}
		return "delRole";
	}
	/**
	 * 准备编辑角色
	 * @return
	 */
	public String pEditRole(){
		try {
			role = roleService.findRoleById(role.getRId());
		} catch (Exception e) {
			log.error("跳转到编辑角色页面异常.",e);
		}
		return "pEditRole";
	}
	/**
	 * 编辑角色
	 * @return
	 */
	public String editRole(){
		try {
			if(role != null){
				role.setLastModifyTime(DateUtil.formatDate());
				roleService.saveOrUpdateRole(role);
				addActionMessage("修改角色成功!");
			}else{
				addActionMessage("修改角色失败!");
			}
		} catch (Exception e) {
			log.error("编辑角色异常.",e);
		}
		return "editRole";
	}
	
	////////////////////////////////////////////////////////
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	public String getCheckItems() {
		return checkItems;
	}
	public void setCheckItems(String checkItems) {
		this.checkItems = checkItems;
	}
	public SRole getRole() {
		return role;
	}
	public void setRole(SRole role) {
		this.role = role;
	}
	public List<Map<String, String>> getAllResGroup() {
		return allResGroup;
	}
	public void setAllResGroup(List<Map<String, String>> allResGroup) {
		this.allResGroup = allResGroup;
	}
	public String getAllGroupedRes() {
		return allGroupedRes;
	}
	public void setAllGroupedRes(String allGroupedRes) {
		this.allGroupedRes = allGroupedRes;
	}
	public List<Map<String, String>> getNoGroupedRes() {
		return noGroupedRes;
	}
	public void setNoGroupedRes(List<Map<String, String>> noGroupedRes) {
		this.noGroupedRes = noGroupedRes;
	}
	public String getAllRolesRes() {
		return allRolesRes;
	}
	public void setAllRolesRes(String allRolesRes) {
		this.allRolesRes = allRolesRes;
	}
}
